//package com.yunhe.device.service.device.impl;
//
//import com.yunhe.common.model.PageParam;
//import com.yunhe.device.domain.device.ConnectivityNode;
//import com.yunhe.device.domain.device.Terminal;
//import com.yunhe.common.exception.ArgumentErrorException;
//import com.yunhe.common.exception.ObjectNotFoundException;
//import com.yunhe.common.exception.UniqueConstraintsException;
//import com.yunhe.device.repository.authority.ResourceTreeRepository;
//import com.yunhe.device.repository.device.ConnectivityNodeRepository;
//import com.yunhe.device.repository.device.TerminalRepository;
//import com.yunhe.device.service.device.ModelService;
//import com.yunhe.device.service.device.TerminalService;
//import com.yunhe.common.util.QueryUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Sort;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.List;
//import java.util.Optional;
//
///**
// * 端子Service
// * @desc 端子服务类实现
// * @author liuronglei
// */
//@Service
//public class TerminalServiceImpl implements TerminalService {
//    @Autowired
//    TerminalRepository terminalRepository;
//    @Autowired
//    ConnectivityNodeRepository connectivityNodeRepository;
//    @Autowired
//    private ModelService modelService;
//    @Autowired
//    private ResourceTreeRepository resourceTreeRepository;
//
//    /**
//     * 端子对象处理和判断
//     * @param terminal 端子对象
//     */
//    private void processBeforeSave(Terminal terminal)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        //判断代码和connectivityNodeId是否为空
//        if (terminal.getCode() == null || terminal.getConnectivityNodeCode() == null) {
//            throw new ArgumentErrorException("代码和连接节点代码不能为空");
//        }
//        //判断connectivityNodeCode是否存在
//        ConnectivityNode cn = connectivityNodeRepository.findByCode(terminal.getConnectivityNodeCode());
//        if (cn == null) {
//            throw new ObjectNotFoundException("找不到代码为" + terminal.getConnectivityNodeCode() + "的ConnectivityNode");
//        } else {
//            terminal.setConnectivityNodeId(cn.getId());
//        }
//        //判断代码是否已存在
//        Terminal terminalByCode = this.findByCode(terminal.getCode());
//        if (terminalByCode != null) {
//            if (terminal.getId() == null || terminalByCode.getId().longValue() != terminal.getId().longValue()) {
//                throw new UniqueConstraintsException("该代码已存在");
//            }
//        }
//    }
//
//    /**
//     * 新增端子
//     * @param terminal 新增对象
//     */
//    @Override
//    @Transactional
//    public Terminal createTerminal(Terminal terminal)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        terminal.setId(null);
//        processBeforeSave(terminal);
//        //新增端子
//        Terminal newTerminal = terminalRepository.save(terminal);
////        resourceTreeRepository.bindResourceByFirmCode(firmCode, "Terminal", newTerminal.getId());
////        svgCode.ifPresent(value -> modelService.addLabel(newTerminal.getId(), value));
//        //添加关系
//        terminalRepository.addConnectivityNodeRelation(newTerminal.getId(), terminal.getConnectivityNodeId());
//        return newTerminal;
//    }
//
//    /**
//     * 批量新增端子
//     * @param terminalList 端子列表
//     */
//    @Override
//    @Transactional
//    public Iterable<Terminal> createTerminal(List<Terminal> terminalList)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        for (Terminal terminal : terminalList) {
//            terminal.setId(null);
////            //判断代码是否为空
////            if (terminal.getCode() == null) {
////                throw new ArgumentErrorException("代码不能为空");
////            }
////            //判断代码是否已存在
////            Terminal terminalByCode = this.findByCode(terminal.getCode());
////            if (terminalByCode != null) {
////                throw new UniqueConstraintsException("代码 " + terminal.getCode() + " 已存在");
////            }
//            processBeforeSave(terminal);
//        }
//        //新增端子
//        Iterable<Terminal> newTerminals = terminalRepository.saveAll(terminalList);
////        for (Terminal terminal : newTerminals) {
////            resourceTreeRepository.bindResourceByFirmCode(firmCode, "Terminal", terminal.getId());
////            svgCode.ifPresent(value -> modelService.addLabel(terminal.getId(), value));
////        }
//        List<Terminal> newTerminalList = (List)newTerminals;
//        //添加关系
//        for (Terminal terminal : newTerminalList) {
//            //判断connectivityNodeCode是否存在
//            ConnectivityNode cn = connectivityNodeRepository.findByCode(terminal.getConnectivityNodeCode());
//            if (cn == null) {
//                //注释掉CN不存在时的报错，因为有可能出现新增端子时，CN确实还没新增
//                //此时，由CN新增时添加关系
//                //throw new ObjectNotFoundException("找不到代码为" + terminal.getConnectivityNodeCode() + "的ConnectivityNode");
//            } else {
//                terminalRepository.addConnectivityNodeRelation(terminal.getId(), cn.getId());
//            }
//        }
//        return newTerminals;
//    }
//
//    /**
//     * 根据id删除端子
//     * @param terminalId 端子ID
//     */
//    @Override
//    @Transactional
//    public void deleteTerminal(Long terminalId) {
//        terminalRepository.deleteById(terminalId);
//    }
//
//    /**
//     * 根据代码删除端子
//     * @param code 代码
//     */
//    @Override
//    @Transactional
//    public Long deleteTerminalByCode(Long code) {
//        return terminalRepository.deleteByCode(code);
//    }
//
//    /**
//     * 更新端子
//     * @param terminal 要更新的端子对象
//     */
//    @Override
//    @Transactional
//    public Terminal updateTerminal(Terminal terminal)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        if (terminal.getCode() == null) {
//            throw new ArgumentErrorException("代码不能为空");
//        }
//        Terminal terminalById = this.findByTerminalId(terminal.getId());
//        if (terminalById == null) {
//            throw new ObjectNotFoundException("找不到要修改的端子");
//        }
//        Terminal terminalByCode = this.findByCode(terminal.getCode());
//        if (terminalByCode != null && terminalByCode.getId().longValue() != terminal.getId().longValue()) {
//            throw new UniqueConstraintsException("该代码已存在");
//        }
//        Terminal newTerminal = terminalRepository.save(terminal);
//        //更新连接节点
//        if (terminalById.getConnectivityNodeCode() != terminal.getConnectivityNodeCode()) {
//            terminalRepository.delConnectivityNodeRelation(terminal.getId(), terminalById.getConnectivityNodeCode());
//            if (terminal.getConnectivityNodeCode() != null) {
//                terminalRepository.addConnectivityNodeRelation(terminal.getId(), terminal.getConnectivityNodeCode());
//            }
//        }
//        return newTerminal;
//    }
//
//    /**
//     * 更新端子
//     * @param terminalId 端子ID
//     * @param code 代码
//     * @param devCode 设备代码
//     * @param connectivityNodeCode 连接节点代码
//     */
//    @Override
//    @Transactional
//    public Terminal updateTerminal(Long terminalId,
//                                   Long code,
//                                   Long devCode,
//                                   Long connectivityNodeCode)
//            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
//        Terminal terminal = this.findByTerminalId(terminalId);
//        if (terminal == null) {
//            throw new ObjectNotFoundException("找不到要修改的设备");
//        }
//        if (code != null) {
//            Terminal terminalByCode = this.findByCode(code);
//            if (terminalByCode != null && terminalByCode.getId().longValue() != terminalId.longValue()) {
//                throw new UniqueConstraintsException("该代码已存在");
//            }
//        }
//        Optional.ofNullable(code).ifPresent(terminal::setCode);
//        Optional.ofNullable(devCode).ifPresent(terminal::setDevCode);
//        Optional.ofNullable(connectivityNodeCode).ifPresent(terminal::setConnectivityNodeCode);
//        Terminal newTerminal = terminalRepository.save(terminal);
//        //更新连接节点
//        if (connectivityNodeCode != null && !connectivityNodeCode.equals(terminal.getConnectivityNodeCode())) {
//            terminalRepository.delConnectivityNodeRelation(terminalId, terminal.getConnectivityNodeCode());
//            terminalRepository.addConnectivityNodeRelation(terminalId, connectivityNodeCode);
//        }
//        return newTerminal;
//    }
//
//    /**
//     * 根据ID获得端子对象
//     * @param terminalList 端子ID
//     */
//    @Override
//    public Terminal findByTerminalId(Long terminalList) {
//        return terminalRepository.findById(terminalList).orElse(null);
//    }
//
//    /**
//     * 根据代码获得端子对象
//     * @param code 代码
//     */
//    @Override
//    public Terminal findByCode(Long code) {
//        return terminalRepository.findByCode(code);
//    }
//
//    /**
//     * 获得所有端子对象
//     * @param pageParam 分页对象
//     */
//    @Override
//    public Iterable<Terminal> findTerminals(PageParam pageParam) {
//        Iterable<Terminal> results;
//        if (QueryUtil.needPaging(pageParam)) {
//            results = terminalRepository.findAll(QueryUtil.getPageRequest(pageParam));
//        } else {
//            Sort sort = pageParam == null ? null : QueryUtil.getSort(pageParam.getSort());
//            if (sort != null) {
//                results = terminalRepository.findAll(sort);
//            } else {
//                results = terminalRepository.findAll();
//            }
//        }
//        return results;
//    }
//
//}
